/**
 * pages模版快速生成脚本,执行命令 npm run tep `文件名`
 */

import { mkdirSync, writeFileSync } from 'fs';

const dirName = process.argv[2];

if (!dirName) {
    console.log('文件夹名称不能为空！');
    console.log('示例：npm run tep test');
    process.exit(0);
}

// 页面模版
const indexTep = `
import Taro from '@tarojs/taro'
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { View } from '@tarojs/components'
import './index.scss'

@connect(({${dirName}}) => ({ ...${dirName}}))
export default class ${titleCase(dirName)} extends Component {
  onReady() {
    this.dispatch('list', {})
  }
  dispatch = (domain, data) => {
    this.props.dispatch({
      type: '${dirName}/' + domain,
      payload: data,
    })
  }

  render() {
    const { list } = this.state
    return (
      <View className='${dirName}'>
      </View>
    )
  }
}
`;

// scss文件模版
const scssTep = `
.page {
}
`;

mkdirSync(`./src/pages/${dirName}`); // mkdir $1
process.chdir(`./src/pages/${dirName}`); // cd $1

writeFileSync('index.tsx', indexTep);
writeFileSync('index.module.scss', scssTep);

console.log(`模版${dirName}已创建,请手动增加models`);

function titleCase(str) {
    const array = str.toLowerCase().split(' ');
    for (let i = 0; i < array.length; i++) {
        array[i] = array[i][0].toUpperCase() + array[i].substring(1, array[i].length);
    }
    const string = array.join(' ');
    return string;
}

process.exit(0);
